package cc.ruih.meizanchen;

import java.util.Calendar;

import cc.ruih.meizanchen.R;
import cc.ruih.meizanchen.mm.MMIndex;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 妈妈登录页面
 * @author 郭瑞坡
 *
 */
public class MMLogin extends Activity {
	
	Button confirm;
	EditText mmmz;
	EditText ycq;
	private int mYear;
	private int mMonth;
	private int mDay;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mm_login);
		mmmz = (EditText) findViewById(R.id.mmmz);
		ycq = (EditText) findViewById(R.id.ycq);
		confirm = (Button) findViewById(R.id.confirm);
		
		// 默认初始预产期
		final Calendar c = Calendar.getInstance(); 
		mYear=c.get(Calendar.YEAR);
		mMonth=c.get(Calendar.MONTH);
		mDay=c.get(Calendar.DAY_OF_MONTH);
		ycq.setText(mYear+"年"+(mMonth+1)+"月"+mDay+"日");	
		ycq.setOnClickListener(new YcqClick());
		//ycq.clearFocus();
		ycq.setInputType(InputType.TYPE_NULL);
		// ycq.setSelection(3)  设置光标的位置
		confirm.setOnClickListener(new ButtonClick());
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			Intent main=new Intent();
			main.setClass(getApplicationContext(), Main.class);		// 跳到入口页
			startActivity(main);
		}
		return super.onKeyDown(keyCode, event);
	}
	
	//创建DatePickerDialog
	protected Dialog createTimePickerDialog() {
		return new DatePickerDialog(this,
                mDateSetListener, mYear, mMonth, mDay);
	}
	
	//DatePickerDialog上的监听
	private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker view, int year, int month,
                    int dayOfMonth) {
            	ycq.setText(new StringBuilder()
        	                    .append(year).append("年")
        	                    .append(month + 1).append("月")
        	                    .append(dayOfMonth).append("日"));
            	mYear=year;
            	mMonth=month;
            	mDay=dayOfMonth;
            }
        };
	
	private class YcqClick implements OnClickListener {

		@Override
		public void onClick(View v) {
			createTimePickerDialog().show();
		}	// OnTouchListener {
		/*@Override
		public boolean onTouch(View v, MotionEvent event) {
			createTimePickerDialog().show();
			return false;
		}*/
	}
	
	private class ButtonClick implements OnClickListener {
		@Override
		public void onClick(View v) {
			if(mmmz.getText().toString() != null && mmmz.getText().toString() != "" && mmmz.getText().toString().length() > 0
					&& ycq.getText().toString() != null && ycq.getText().toString() != "" && ycq.getText().toString().length() > 0) {
				SharedPreferences sharedPreferences = getSharedPreferences("mminfo", Context.MODE_PRIVATE);
				Editor editor = sharedPreferences.edit();	//获取编辑器
				editor.putString("name", mmmz.getText().toString());
				editor.putString("ycq", ycq.getText().toString());
				editor.commit();
				Intent intent = new Intent();
				intent.setClass(MMLogin.this, MMIndex.class);	// 跳转到孕妈妈首页
				startActivity(intent);
			} else {
				Toast.makeText(getApplicationContext(), "妈妈的名字和预产期不能为空", Toast.LENGTH_LONG).show();
				return;
			}
			
		}
    }
}
